package solution1.demo.demo16;

import java.util.Arrays;

public class Solution {

    /**
     * 插入排序
     * @param arr
     */
    public void insertSort(int[] arr) {
        for(int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if(arr[j] > key) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = key;
        }
    }

    /**
     * 希尔排序
     * @param arr
     */
    public void shellSort(int[] arr) {
        int ans = arr.length;
        while(ans > 1) {
            ans >>= 1;
            shell(arr, ans);
        }
        shell(arr, 1);
    }

    private void shell(int[] arr, int ans) {
        for(int i = ans; i < arr.length; i++) {
            int val = arr[i];
            int j = i - ans;
            for(; j >= 0; j -= ans) {
                if(arr[j] > val) {
                    arr[j + ans] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + ans] = val;
        }
    }

    /**
     * 冒泡排序
     * @param arr
     */
    public void bubbleSort(int[] arr) {
        for(int i = 0; i < arr.length - 1; i++) {
            for(int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
    }

    /**
     * 选择排序
     * @param arr
     */
    public void selectSort(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for(int j = i + 1; j < arr.length; j++) {
                if(arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }

    private void swap(int[] arr, int i, int minIndex) {
        int tmp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = tmp;
    }

    /**
     * 堆排序
     * @param arr
     */
    public void heapSort(int[] arr) {
        //创建大根堆
        createBigHeap(arr);
        int end = arr.length - 1;
        while(end > 0) {
            swap(arr, 0, end);
            adjustDown(arr, 0, end);
            end--;
        }
    }

    private void createBigHeap(int[] arr) {
        int len = arr.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--) {
            adjustDown(arr, parent , len);
        }
    }

    private void adjustDown(int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while(child < len) {
            if(child + 1 < len && arr[child] < arr[child + 1]) {
                child++;
            }
            if(arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }

    /**
     * 快速排序
     * @param arr
     */
    public void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private void quick(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        if(right - left + 1 <= 7) {
            quickInsert(arr, left, right);
        }
        //三数取中
        int mid = fundMidIndex(arr, left, right);
        swap(arr, left, mid);
        //寻找基准
        int point = fundPoint(arr, left, right);
        quick(arr, left, point - 1);
        quick(arr, point + 1, right);
    }

    private void quickInsert(int[] arr, int left, int right) {
        for(int i = left + 1; i <= right; i++) {
            int key = arr[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if(arr[j] > key) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = key;
        }
    }

    private int fundMidIndex(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if(arr[left] < arr[right]) {
            if(arr[right] < arr[mid]) {
                return right;
            } else if(arr[left] > arr[mid]) {
                return left;
            } else {
                return mid;
            }
        } else {
            if(arr[left] < arr[mid]) {
                return left;
            } else if(arr[right] > arr[mid]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    private int fundPoint(int[] arr, int left, int right) {
        int key = arr[left];
        while(left < right) {
            while(left < right && arr[right] >= key) {
                right--;
            }
            arr[left] = arr[right];
            while(left < right && arr[left] <= key) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = key;
        return left;
    }

    /**
     * 归并排序
     * @param arr
     */
    public void mergeSort(int[] arr) {
        mergeFun(arr, 0 ,arr.length - 1);
    }

    private void mergeFun(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeFun(arr, left, mid);
        mergeFun(arr, mid + 1, right);
        merge(arr, left, right, mid);
    }

    private void merge(int[] arr, int left, int right, int mid) {
        int[] tmpArr = new int[right - left + 1];
        int index = 0;
        int s1 = left;
        int s2 = mid + 1;
        while(s1 <= mid && s2 <= right) {
            if(arr[s1] < arr[s2]) {
                tmpArr[index++] = arr[s1++];
            } else {
                tmpArr[index++] = arr[s2++];
            }
        }
        while(s1 <= mid) {
            tmpArr[index++] = arr[s1++];
        }
        while(s2 <= right) {
            tmpArr[index++] = arr[s2++];
        }
        //合并
        for(int i = 0; i < index; i++) {
            arr[left + i] = tmpArr[i];
        }
    }

}

class Main {
    public static void main(String[] args) {
        int[] arr = {3,1,2,8,6,5,4,1,7};
        Solution solution = new Solution();
        solution.mergeSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

